package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.entity.ProSize;
import com.mall.product.domain.entity.ProSizeGroup;
import com.mall.product.service.ProSizeGroupService;
import com.mall.product.service.ProSizeService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 尺码Controller
 *
 * @author zhonglin
 * @date 2020-04-29
 */
@Controller
@RequestMapping("/product/size")
public class ProSizeController
    extends BaseController {

  private String prefix = "product/size" ;

  @Autowired
  private ProSizeService      proSizeService;
  @Autowired
  private ProSizeGroupService proSizeGroupService;

  @RequiresPermissions("product:size:view")
  @GetMapping()
  public String size() {

    return prefix + "/size" ;
  }

  /**
   * 查询尺码列表
   */
  @RequiresPermissions("product:size:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProSize proSize) {

    startPage();
    List<ProSize> list = proSizeService.selectProSizeList(proSize);
    return getDataTable(list);
  }

  /**
   * 导出尺码列表
   */
  @RequiresPermissions("product:size:export")
  @Log(title = "尺码", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProSize proSize) {

    List<ProSize>      list = proSizeService.selectProSizeList(proSize);
    ExcelUtil<ProSize> util = new ExcelUtil<ProSize>(ProSize.class);
    return util.exportExcel(list, "size");
  }

  /**
   * 新增尺码
   */
  @GetMapping("/add")
  public String add(ModelMap mmap) {

    mmap.put("sizeGroups", proSizeGroupService.selectSizeGroupAll());
    return prefix + "/add" ;
  }

  /**
   * 新增保存尺码
   */
  @RequiresPermissions("product:size:add")
  @Log(title = "尺码", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProSize proSize) {

    return toAjax(proSizeService.insertProSize(proSize));
  }

  /**
   * 修改尺码
   */
  @GetMapping("/edit/{sizeId}")
  public String edit(@PathVariable("sizeId") Long sizeId,
                     ModelMap mmap) {

    ProSize proSize = proSizeService.selectProSizeById(sizeId);
    ProSizeGroup proSizeGroup = proSizeGroupService.selectProSizeGroupById(proSize.getSizeGroupId());
    mmap.put("proSize", proSize);
    mmap.put("sizeGroup", proSizeGroup);
    return prefix + "/edit" ;
  }

  /**
   * 修改保存尺码
   */
  @RequiresPermissions("product:size:edit")
  @Log(title = "尺码", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProSize proSize) {

    return toAjax(proSizeService.updateProSize(proSize));
  }

  /**
   * 删除尺码
   */
  @RequiresPermissions("product:size:remove")
  @Log(title = "尺码", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proSizeService.deleteProSizeByIds(ids));
  }

}
